package csu.rjxyg.action.fileAction;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import csu.rjxyg.action.BaseAction;
import csu.rjxyg.action.constant.UserConstant;
import csu.rjxyg.bean.core.BaseBean;
import csu.rjxyg.bean.core.BaseObject;
import csu.rjxyg.factory.core.UtilFactory;
import csu.rjxyg.service.BaseUserService;
import csu.rjxyg.util.DataUtil;
import csu.rjxyg.util.LogUtil;

public class ExcelAction extends BaseAction{
	private LogUtil log;
	private DataUtil data;
	
	private File file; //上传的文件
	private String fileContentType;
	private String fileFileName;

	
    public ExcelAction(){
    	log = UtilFactory.getLogUtil(this.getClass());
    }
    
	public void userFileUpload() throws IOException{
		String name = null;
		try {
			BaseUserService userService = (BaseUserService) factory.getBean(BaseAction.USER_SERVICE);
			BaseObject object = (BaseObject) this.getSession().get(UserConstant.USER_SESSION);
			name = (String) object.get(UserConstant.USER_ID);

			userService = (BaseUserService) factory.getBean(BaseAction.USER_SERVICE);
			data = UtilFactory.getDataUtil();
			String[][] userData= data.getData(file, 4);
			for( int i = 0 ; i < userData.length;i++ ){
				String userId = userData[i][0];
				String userName = userData[i][1];
				String userPass = userData[i][2];
				String[] temp = userData[i][3].split("-");
				int[] permissions = this.getPermissionUpAndDown(Integer.parseInt((String) object.get(UserConstant.USER_LOGIN_QX)));
				int num = temp.length;
				for( int j = 0;j <  temp.length;j++){
					int k = Integer.parseInt(temp[j]);
					
					if (k< permissions[0]  ||k > permissions[1]){
						temp[j] = "-1";
						num--;
					}
				}
				String[] userPermission = new String[num];
				
				if( num == 0 ){
					throw new Exception();
				}
				num = 0;
				for( int k = 0; k < temp.length;k++ ){
					if( !temp[k].equals("-1") ){
						userPermission[num] = temp[k];
						num++;
					}
				}
				
				BaseObject tempObject = (BaseObject) factory
						.getBean(BaseBean.BEAN_OBJECT);

				tempObject.set(UserConstant.USER_ID, userId);
				tempObject.set(UserConstant.USER_PASS, userPass);
				tempObject.set(UserConstant.USER_NAME, userName);
				tempObject.set(UserConstant.USER_QX, userPermission);
				tempObject.set(UserConstant.USER_LOGIN_QX, userPermission[0]);

				if (userService.insertUserBean(tempObject)) {
					log.info(name + "新建用户信息成功，信息如下: " + tempObject.getBean());
				}else{
					log.info(name + "新建用户信息失败");
					this.out("3");
					return;
				}
			}
			
			
			
		} catch (Exception e) {
			this.out("3");
			log.info(name + "新建用户信息失败");
			e.printStackTrace();
			log.info(e.getMessage());
		}
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	
	
	
	
}
